﻿using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Data;


/// <summary>
///MakeHtml 的摘要说明
/// </summary>
public class MakeHtml
{
	public MakeHtml()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    /// <summary>
    /// 生成信息页面
    /// </summary>
    /// <param name="id"></param>
    public static String info(String template,String id,String target) {
        //查询此id数据
        DataTable dt = DB.getDataTable("select * from news where newid="+id);
        if (dt != null && dt.Rows.Count > 0)
        {
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = null;

            //读取
            try
            {
                sr = new StreamReader(template);
                str = sr.ReadToEnd();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sr.Close();
            }

            //根据时间自动重命名，扩展名也可以自行修改
            string fileName = id + ".htm";
            String str1 = "";
            String sql1 = "select top 1 name,father_id,url,pageLocation from channel where  channel_id=" + dt.Rows[0]["channelid"].ToString();

            DataTable dt1 = DB.getDataTable(sql1);
            if (dt1.Rows.Count > 0)
            {
                if (dt1.Rows[0]["father_id"] != null && Int16.Parse(dt1.Rows[0]["father_id"].ToString()) > 1)
                {
                    String sql11 = "select top 1 name,url,pageLocation from channel where  channel_id=" + dt1.Rows[0]["father_id"];

                    DataTable dt11 = DB.getDataTable(sql11);
                    if (dt11.Rows.Count > 0)
                    {
                        str1 += "<a href='../" + dt11.Rows[0]["url"].ToString() +
                            (dt11.Rows[0]["url"].ToString().IndexOf("?") > -1 ? "&" : "?") + "channel=" +
                            dt11.Rows[0]["pageLocation"].ToString() + "'>" + dt11.Rows[0]["name"].ToString() + "</a>  -> ";
                    }
                }
                str1 += "<a href='../" + dt1.Rows[0]["url"].ToString() +
                    (dt1.Rows[0]["url"].ToString().IndexOf("?") > -1 ? "&" : "?") + "channel=" +
                    dt1.Rows[0]["pageLocation"].ToString() + "'>" + dt1.Rows[0]["name"].ToString() + "</a>  -> ";
            }
            str1 += dt.Rows[0]["title"].ToString();
            str = str.Replace("$title$", dt.Rows[0]["title"].ToString());//替换Title
            str = str.Replace("$location$", str1);//替换location
            str = str.Replace("$content$", dt.Rows[0]["content"].ToString());//替换content

            //生成静态文件
            try
            {
                sw = new StreamWriter(target + fileName, false);
                sw.Write(str);
                sw.Flush();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sw.Close();
            }
            return str;
        }
        return "no";
    }
}